<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="活动名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="活动名称"></el-input>
    </el-form-item>
    <el-form-item label="活动编号" prop="code">
      <el-input v-model="dataForm.code" placeholder="活动名称"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="startTime">
      <el-date-picker
        v-model="dataForm.startTime"
        type="date"
        placeholder="选择开始时间">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="结束时间" prop="endTime">
      <el-date-picker
        v-model="dataForm.endTime"
        type="date"
        placeholder="选择结束时间">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="状态" prop="state">
      <el-switch
        v-model="dataForm.state"
        active-color="#13ce66"
        inactive-color="#dcdfe6">
      </el-switch>
    </el-form-item>    
    
    <!-- <el-form-item label="状态" prop="state">
      <el-input v-model="dataForm.state" placeholder="活动状态: 1:启用 0:禁用"></el-input>
    </el-form-item> -->
    <!-- <el-form-item label="创建时间" prop="createdAt">
      <el-input v-model="dataForm.createdAt" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updatedAt">
      <el-input v-model="dataForm.updatedAt" placeholder="更新时间"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          code: '',
          name: '',
          startTime: '',
          endTime: '',
          state: true,
          createdAt: '',
          updatedAt: ''
        },
        dataRule: {
          name: [
            { required: true, message: '活动名称不能为空', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '活动编号不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '活动开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '活动结束时间不能为空', trigger: 'blur' }
          ]/*,
          createdAt: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updatedAt: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]*/
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/biz/couponActivity/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.couponActivity.name
                this.dataForm.state = data.couponActivity.state
                this.dataForm.createdAt = data.couponActivity.createdAt
                this.dataForm.updatedAt = data.couponActivity.updatedAt
              }
            })
          }
        })
      },
      formatData(initData) {
          //初始化选择的时间  
          var d=new Date(initData); 

          var month = d.getMonth() + 1; 
          if (month < 10) {
              month = '0' + month;
          }
          //如果天数小于10号，则在前面加0  
          var day=d.getDate()
          if (day < 10) {  
              day = '0'+ day;  
          }  

          return d.getFullYear() + '-' + month + '-' + day;
      },
      // 表单提交
      dataFormSubmit () {
        console.log(this.formatData(this.dataForm.startTime));
        console.log(this.formatData(this.dataForm.endTime));
        //return;
        var flag = 0;
        if (this.dataForm.state) {
          flag = 1;
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/biz/couponActivity/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'code': this.dataForm.code,
                'startTime': this.formatData(this.dataForm.startTime) + " 00:00:00",
                'endTime': this.formatData(this.dataForm.endTime) + " 23:59:59",
                'state': flag
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
